package com.yikaiye.android.yikaiye.application;

import android.app.Activity;
import android.app.Application;
import android.content.Context;

import androidx.multidex.MultiDex;

import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.footer.ClassicsFooter;
import com.scwang.smartrefresh.layout.header.ClassicsHeader;
import com.tencent.mm.opensdk.openapi.IWXAPI;
import com.tencent.mm.opensdk.openapi.WXAPIFactory;
import com.umeng.socialize.PlatformConfig;
import com.yikaiye.android.yikaiye.config.Constant;
import com.yikaiye.android.yikaiye.config.UserManager;
import com.yikaiye.android.yikaiye.di.AppComponent;
import com.yikaiye.android.yikaiye.di.AppModule;
import com.yikaiye.android.yikaiye.di.DaggerAppComponent;
import com.yikaiye.android.yikaiye.di.retrofit.RetrofitModule;
import com.yikaiye.android.yikaiye.utils.ActivityStackManager;

public class MyApplication extends Application {
    private static MyApplication instance;
    private static Context context;
    private AppComponent mAppComponent;
    public static UserManager manager;
    public static IWXAPI wxapi;

    @Override
    public void onCreate() {
        super.onCreate();
        context = getApplicationContext();
        instance = this;
        initComponent();
        manager = UserManager.getInstance();
        MultiDex.install(this);
        wxapi = WXAPIFactory.createWXAPI(this, Constant.WE_CHAT_APP_ID, true);
        // 将应用的appId注册到微信
        wxapi.registerApp(Constant.WE_CHAT_APP_ID);
        // 微信设置
        PlatformConfig.setWeixin(Constant.WE_CHAT_APP_ID, Constant.WE_CHAT_APP_SECRET);
        PlatformConfig.setWXFileProvider("com.yikaiye.android.yikaiye.fileprovider");

//        // SDK预初始化函数
//        // preInit预初始化函数耗时极少，不会影响App首次冷启动用户体验
//        UMConfigure.preInit(this,Constant.UMENG_APP_KEY,"umeng");
//
//        // 第一次冷启动
////        boolean isFirst = SPUtil.getBoolean("isFirst", true);
////        if (!isFirst) {
//            /*
//             * 注意: 即使您已经在AndroidManifest.xml中配置过appkey和channel值，也需要在App代码中调
//             * 用初始化接口（如需要使用AndroidManifest.xml中配置好的appkey和channel值，
//             * UMConfigure.init调用中appkey和channel参数请置为null）。
//             */
////            UMConfigure.init(this, Constant.UMENG_APP_KEY, "umeng", UMConfigure.DEVICE_TYPE_PHONE, "");
////        }
//
//        //选择AUTO页面采集模式，统计SDK基础指标无需手动埋点可自动采集。
//        //建议在宿主App的Application.onCreate函数中调用此函数。
//        MobclickAgent.setPageCollectionMode(MobclickAgent.PageMode.AUTO);
//
////        PushManager.getInstance().initialize(context);
//        // release版去掉
////        PushManager.getInstance().setDebugLogger(this, new IUserLoggerInterface() {
////            @Override
////            public void log(String s) {
////                Log.e("PUSH_LOG",s);
////            }
////        });
    }

    public static Context getContext() {
        return context;
    }

    public static MyApplication getInstance() {
        return instance;
    }

    private void initComponent() {
        this.mAppComponent = DaggerAppComponent
                .builder()
                .retrofitModule(new RetrofitModule())
                .appModule(new AppModule(this))
                .build();
    }

    public AppComponent getAppComponent() {
        return this.mAppComponent;
    }

    static {
        //设置全局的Header和Footer构建器
        SmartRefreshLayout.setDefaultRefreshHeaderCreator((context, layout) -> new ClassicsHeader(context));
        SmartRefreshLayout.setDefaultRefreshFooterCreator((context, layout) -> new ClassicsFooter(context));
    }

    //添加Activity到容器中
    public void addActivity(Activity activity) {
        ActivityStackManager.getInstance().pushActivity(activity);
    }

    //遍历所有Activity并finish
    public void exit() {
        ActivityStackManager.getInstance().exitActivity();
    }

    //弹出除一个以外的所有活动
    public void popAllActivityExceptOne() {
        ActivityStackManager.getInstance().popAllActivityExceptMain();
    }

    //退出app
    public void exitApp() {
        ActivityStackManager.getInstance().exitActivity();
        System.exit(0);
    }
}
